{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课程1.1---Numpy基础数据结构\n",
    "     \n",
    "## Numpy数组是一个多维数组对象，称为ndarray，它由两部分组成：\n",
    "* 实际的数据\n",
    "* 描述这些数据的元数据\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7]\n",
      "1\n",
      "(7,)\n",
      "7\n",
      "int32\n",
      "4\n",
      "<memory at 0x000001C95C2B5108>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多维数组ndarray\n",
    "import numpy as np\n",
    "\n",
    "ar = np.array([1,2,3,4,5,6,7])\n",
    "print(ar)          # 输出数组，注意数组的格式：中括号，元素中间没有逗号（和列表区分）\n",
    "print(ar.ndim)     # 输出数组维度的个数（轴数），或者说“秩”，维度的数量也称 rank\n",
    "print(ar.shape)    # 数组的维度，对于n行m列的数组，shape为（n，m）\n",
    "print(ar.size)     # 数组的元素总数，对于n行m列的数组，元数总数为n*m\n",
    "print(ar.dtype)    # 数组中元素的类型，类似type()（注意：type()是函数，dtype是方法）\n",
    "print(ar.itemsize) # 数组中每个元素的字节大小，int32类型字节为4，float64类型字节为8\n",
    "print(ar.data)     #包含实际数组元素的缓冲区，由于一般通过数组的索引获取元素，所以通常不需要使用这个属性\n",
    "ar                 #交互方式下输出，会有array（数组）\n",
    "\n",
    "# 数组的基本属性\n",
    "# 1.数组的维数称为秩（rank），一维数组的秩为1，二维数组的秩为2，以此类推\n",
    "# 2.在Numpy中，每一个线性的数组称为一个轴（axes），秩其实是描述轴的数量\n",
    "# 比如说，二维数组相当于是两个一维数组，其中，第一个一维数组中每个元素又是一个一维数组\n",
    "# 所以一维数组就是Numpy中的轴（axes），第一个周相当于是底层数组，第二个轴是底层数组里面的数组\n",
    "# 而轴的数量---秩，就是数组的维数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9] <class 'numpy.ndarray'> int32\n",
      "[1.   2.   3.14 4.   5.  ] <class 'numpy.ndarray'> float64\n",
      "[['1' '2' '3']\n",
      " ['a' 'b' 'c']] (2, 3) 2 6\n",
      "[list([1, 2, 3]) ('a', 'b', 'c', 'd')] (2,) 1 2\n"
     ]
    }
   ],
   "source": [
    "# 创建数组：array()函数，括号内可以是列表、元组，数组，生成器等\n",
    "\n",
    "ar1 = np.array(range(10))        #整型\n",
    "ar2 = np.array([1,2,3.14,4,5])   #浮点型\n",
    "ar3 = np.array([[1,2,3],('a','b','c')])        #二维数组：嵌套序列（列表，元组均可） 注意：如果列表中含有字符串，则相关的数字也会被强制转换成字符串类型\n",
    "ar4 = np.array([[1,2,3],('a','b','c','d')])    #注意嵌套序列数量不一样会怎么样\n",
    "print(ar1,type(ar1),ar1.dtype)\n",
    "print(ar2,type(ar2),ar2.dtype)\n",
    "print(ar3,ar3.shape,ar3.ndim,ar3.size)         #二维数组，共6个元素\n",
    "print(ar4,ar4.shape,ar4.ndim,ar4.size)         #一维数组，共2个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n",
      "[ 5  6  7  8  9 10 11]\n",
      "[ 5.  7.  9. 11.]\n",
      "[   0    1    2 ... 9997 9998 9999]\n"
     ]
    }
   ],
   "source": [
    "# 创建数组：arange(),类似range()，在给定间隔内返回均匀间隔的值\n",
    "\n",
    "print(np.arange(10))                # 返回0-9，整型\n",
    "print(np.arange(10.0))              # 返回0.0-9.0，浮点型\n",
    "print(np.arange(5,12))              # 返回5-11\n",
    "print(np.arange(5.0,12.0,2))          # 返回5.0-12.0，步长为2\n",
    "print(np.arange(10000))             # 如果数组太大而无法打印，Numpy会自动跳过数组的中心部分，并且只打印边角"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2.   2.25 2.5  2.75 3.  ] <class 'numpy.ndarray'>\n",
      "[2.  2.2 2.4 2.6 2.8]\n",
      "(array([2.  , 2.25, 2.5 , 2.75, 3.  ]), 0.25) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 创建数组：linspace()，返回在间隔[开始，停止]上计算的num个均匀间隔的样本\n",
    "\n",
    "ar1 = np.linspace(2.0,3.0,num = 5)\n",
    "ar2 = np.linspace(2.0,3.0,num = 5,endpoint = False)\n",
    "ar3 = np.linspace(2.0,3.0,num = 5,retstep = True)\n",
    "print(ar1,type(ar1))\n",
    "print(ar2)\n",
    "print(ar3,type(ar3))\n",
    "\n",
    "# numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)\n",
    "# start：起始值   stop：结束值\n",
    "# num：生成样本数，默认为50\n",
    "# endpoint：如果为真，则停止是打印(输出)最后一个样本，否则，不包括在内，默认为True\n",
    "# retstep：如果为真，返回(样本，步骤)，其中步长是样本之间的间距--->输出一个包含两个元素的元组，第一个元素为array,第二个元素为步长实际值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0.] float64\n",
      "[[0 0]\n",
      " [0 0]] int32\n",
      "---------------------------\n",
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "[[0 0 0 0 0]\n",
      " [0 0 0 0 0]]\n",
      "--------------------------\n",
      "[1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      "[[[1. 1. 1. 1.]\n",
      "  [1. 1. 1. 1.]\n",
      "  [1. 1. 1. 1.]]\n",
      "\n",
      " [[1. 1. 1. 1.]\n",
      "  [1. 1. 1. 1.]\n",
      "  [1. 1. 1. 1.]]]\n",
      "[[1 1 1 1 1]\n",
      " [1 1 1 1 1]]\n"
     ]
    }
   ],
   "source": [
    "# 创建数组：zeros()/zeros_like()/ones()/ones_like()\n",
    "\n",
    "ar1 = np.zeros(5)\n",
    "ar2 = np.zeros((2,2),dtype = np.int)\n",
    "print(ar1,ar1.dtype)\n",
    "print(ar2,ar2.dtype)\n",
    "print(\"---------------------------\")\n",
    "# numpy.zeros(shape, dtype = float, order = 'C')   返回给定形状和类型的新数组，并且用零填充\n",
    "# shape：数组维度，二维以上需要用()，且输入参数为整数\n",
    "# dtype：数据类型，默认为numpy.float64\n",
    "# order：是否在存储器中以C或Fortran连续（按行或者列方式）存储多维数据\n",
    "\n",
    "ar3 = np.array([list(range(5)),list(range(5,10))])\n",
    "ar4 = np.zeros_like(ar3)\n",
    "print(ar3)\n",
    "print(ar4)\n",
    "print(\"--------------------------\")\n",
    "# 返回具有与给定数组相同的形状和类型的零数组，这里ar4根据ar3的形状和类型和dtype创建一个全零的数组\n",
    "\n",
    "ar5 = np.ones(9)\n",
    "ar6 = np.ones((2,3,4))\n",
    "ar7 = np.ones_like(ar3)\n",
    "print(ar5)\n",
    "print(ar6)\n",
    "print(ar7)\n",
    "# ones()/ones_like()和zeros()/zeros_like()一样，只是填充为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0. 0. 0.]\n",
      " [0. 1. 0. 0. 0.]\n",
      " [0. 0. 1. 0. 0.]\n",
      " [0. 0. 0. 1. 0.]\n",
      " [0. 0. 0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "# 创建数组：eye()\n",
    "\n",
    "print(np.eye(5))\n",
    "# 创建一个正方的 N * N 的单位矩阵，对角线为1，其余为0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ndarray的数据类型\n",
    "### bool 是用一个字节存储的布尔类型(True或者False)\n",
    "### intc 由其平台决定其大小的整数(通常为int32或者int64)\n",
    "### int8 一个字节大小       -128---127\n",
    "### int16 整数，-32768---32767\n",
    "### int32 整数，-2^31---2^31-1\n",
    "### int64 整数，-2^63---2^63-1\n",
    "### uint8 无符号整数，0---255\n",
    "### uint16 无符号整数，0---65536\n",
    "### uint32 无符号整数， 0---2**32-1\n",
    "### uint64 无符号整数， 0---2**64-1\n",
    "### float16 半精度浮点数：16位，正负号1位，指数5位，精度10位\n",
    "### float32 单精度浮点数：32位，正负号1位，指数8位，精度23位\n",
    "### float64或者float  双精度浮点数：64位，正负号1位，指数11位，精度52位\n",
    "### complex64 复数，分别用两个32位浮点数表示实部和虚部\n",
    "### complex128或者complex 复数，分别用两个64位浮点数表示实部和虚部"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
